/*
 * $Id: SimpleTest4.java 3 2007-12-07 01:24:30Z rasan.rasch $
 *
 * Copyright (c) 2002-2006 UC Regents
 * 
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names
 * of the UC Regents and the University of California are not used in any
 * advertising or publicity relating to the software without the specific,
 * prior written permission of the University of California.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
 * THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE
 * OR PERFORMANCE OF THIS SOFTWARE.
 */

package org.cdl.noid;

import java.io.*;
import java.util.ArrayList;
import java.util.regex.*;
import junit.framework.*;

/** 
 * Equivalent to noid2.t.
 * 
 * @author  Micahel A. Russel
 * @author  Rasan Rasch (rasan@nyu.edu)
 * @version $Revision: 3 $
 */
public class SimpleTest4 extends TestCase {

	public SimpleTest4(String name) {
		super(name);
	}
	
	protected void setUp() {
	
	}

	public void testFoo() {

		Minter minter = new Minter();

		File noidDir = new File("dbnoid");

		if (noidDir.exists()) {
			if (!Util.deleteDir(noidDir)) {
				System.err.println("");
			}
		}
		
		Util.doExec("./runNoid.sh dbcreate tst5.rde long 13030 cdlib.org noidTest >/dev/null");

		assertTrue("dbnoid was created failed", noidDir.isDirectory());

		if (!noidDir.isDirectory()) {
			System.err.println("something is seriously wrong, stopped");
			System.exit(1);
		}

		String[] files = { "README", "log", "logbdb", "noid.bdb" };
		for (int i = 0; i < files.length; i++) {
			File f = new File("dbnoid/" + files[i]);
			assertTrue("dbnoid/" + files[i] + " was not created", f.exists());
		}

		if (!(new File("dbnoid/noid.bdb")).exists()) {
			System.err.println("something is seriously wrong, stopped");
			System.exit(1);
		}

		ArrayList<String> output = new ArrayList<String>();

		// Mint all but the last two of 290.
		Util.doExec("$noid_cmd queue now 13030/tst27h >/dev/null", output);


		output.clear();

		try {
			BufferedReader in = new BufferedReader(new FileReader("dbnoid/log"));

			assertTrue("Successfully opened dbnoid/log", true);
		
			String str;
			while ((str = in.readLine()) != null) {
				output.add(str);
			}
			in.close();
		
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}

		assertEquals("number of lines in dbnoid/log", 4, output.size());


		if (output.size() != 4) {
			System.err.println("log_lines" + output);
			System.exit(1);
		}

		// Check the contents of the lines.
		assertEquals(
			"line 1 of \"dbnoid/log\" correct",
			"Creating database for template \"tst2.rde\".",
			(String) output.get(0)
		);

		assertEquals(
			"line 2 of \"dbnoid/log\" correct",
			"note: id 13030/tst27h being queued before first minting (to be pre-cycled)",
			(String) output.get(1)
		);

		assertTrue(
        	"line 4 of \"dbnoid/log\" correct",
			Util.matches(
        		"m: q|\\d{14}|jak/users|0",
				(String) output.get(2)
			)
		);

		assertTrue(
			"line 4 of \"dbnoid/log\" correct",
			Util.matches(
				"^id: 13030/tst27h added to queue under :/q/",
				(String) output.get(3)
			)
		);
		
	}

}
